cmake_minimum_required(VERSION 3.10)
project(dds_clients)

# 设置C++标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置 CycloneDDS 的路径
set(CYCLONEDDS_HOME "/usr/local/lib/cmake/CycloneDDS/")

# 查找必要的库
find_package(Threads REQUIRED)
# 查找nlohmann_json库
find_package(nlohmann_json REQUIRED)
# 查找 CycloneDDS 库
find_package(CycloneDDS REQUIRED)

file(GLOB SOURCES_CPP "${CMAKE_SOURCE_DIR}/*.cpp")

# -------------------------- 头文件包含目录 --------------------------
include_directories(
    {% for service in services %}
    ${CMAKE_SOURCE_DIR}/../../../atomic_service/{{service["basic_info"]["name"]}}/atomic_include
    ${CMAKE_SOURCE_DIR}/../../../atomic_service/{{service["basic_info"]["name"]}}/idl
    {% endfor %}
    {% for idl in idl_list %}
    ${CMAKE_SOURCE_DIR}/../idl/{{idl}} 
    {% endfor %}
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../../include/log_client
)

# -------------------------- 依赖的C文件收集 --------------------------
# 显式指定所有需要的C文件（根据实际路径调整，确保路径正确）
set(ATOMIC_C_SOURCES
    {% for service in services %}
    ${CMAKE_SOURCE_DIR}/../../../atomic_service/{{service["basic_info"]["name"]}}/idl/{{service["basic_info"]["name"]}}.c
    {% endfor %}
    {% for idl in idl_list %}
    ${CMAKE_SOURCE_DIR}/../idl/{{idl}}/{{idl}}.c
    {% endfor %}
)

link_directories(
    {% for service in services %}
    ${CMAKE_SOURCE_DIR}/../../../atomic_service/{{service["basic_info"]["name"]}}/lib
    {% endfor %}
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../../lib  # 替换为实际的 liblog_client.a 所在目录
)


# -------------------------- 编译C静态库（含IDL实现） --------------------------
add_library(atomic_c_lib STATIC ${ATOMIC_C_SOURCES})
# 链接CycloneDDS的C核心库（ddsc）
target_link_libraries(atomic_c_lib PRIVATE
    CycloneDDS::ddsc
)

# -------------------------- C++主文件配置 --------------------------
# 显式列出6个带main函数的C++文件（替换为你的实际文件名）
set(CPP_MAIN_FILES
    {% for service in services %}
    {{service["basic_info"]["name"]}}_dds_node.cpp
    {% endfor %}
)

# 定义每个可执行文件依赖的静态库（关键映射）
set(EXECUTABLE_LIBS
    {% for service in services %}
    {{service["basic_info"]["name"]}}_dds_node  ${CMAKE_SOURCE_DIR}/../../../atomic_service/{{service["basic_info"]["name"]}}/lib/{{service["basic_info"]["name"]}}.a
    {% endfor %}
)

# -------------------------- 编译可执行文件 --------------------------
foreach(main_file ${CPP_MAIN_FILES})
    get_filename_component(exe_name ${main_file} NAME_WE)  # 提取可执行文件名
    add_executable(${exe_name} ${main_file})
    
    # 查找当前可执行文件对应的静态库
    list(FIND EXECUTABLE_LIBS ${exe_name} lib_index)
    if(lib_index GREATER -1)
        math(EXPR lib_path_index "${lib_index} + 1")
        list(GET EXECUTABLE_LIBS ${lib_path_index} lib_path)
        
        # 链接依赖（包含对应的静态库完整路径）
        target_link_libraries(${exe_name}
            PRIVATE
            atomic_c_lib
            CycloneDDS::ddsc
            Threads::Threads
            ${lib_path}
            logclient
        )
    else()
        message(WARNING "未找到 ${exe_name} 对应的静态库配置")
    endif()
    
    # 设置可执行文件输出目录
    set_target_properties(${exe_name} PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../bin/dds_bin/
        # 设置可执行文件的前缀为空，这样生成的可执行文件名就不会以 lib 开头
        PREFIX ""
    )
endforeach()